package com.CST.controller.zx;


import cn.hutool.core.bean.BeanUtil;
import com.CST.bo.zx.ProjectAllBO;
import com.CST.bo.zx.ProjectDetailBO;
import com.CST.common.api.ResultObject;
import com.CST.dto.zx.ProjectDetailDTO;
import com.CST.service.zx.IZxSumMoneyService;
import com.CST.util.PageHelperTool;
import com.CST.util.ProjectTimeUtil;
import com.CST.vo.zx.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;

import java.math.BigDecimal;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ly
 * @since 2023-03-14
 */
@Api(tags = {"项目档案管理"})
@RestController
@RequestMapping("/zxSumMoney")

public class ZxSumMoneyController {

    @Autowired
    private IZxSumMoneyService targetService;

    @GetMapping("/insertBaseData/{date}")
    @ApiOperation("插入用友数据，设计的是导入当前月的数据，只要当月有数据，就不会导入（月份格式202201）")
    public ResultObject insertBaseData(@PathVariable("date") Integer date) {
        return targetService.insertBaseData(date);
    }

    @PostMapping("/queryForPage")
    @ApiOperation("分页查询项目具体数据,外部界面")
//    只显示从开始时间到结束时间（不包含结束时间）之前的数据
    public ResultObject queryForPage(@RequestBody ProjectAllBO projectAllBO, HttpServletRequest request) {
        List<ProjectAllVO> list = targetService.queryAllProject(projectAllBO,request);
        return ResultObject.success(PageHelperTool.excutePageInfo(list,
                projectAllBO.getPageNo(),
                projectAllBO.getPageSize()));
    }


    @PostMapping("/queryAllProjectDetail")
    @ApiOperation("查询明细信息，模态窗口下半部分页面")
    public ResultObject queryAllProjectDetail(@RequestBody ProjectDetailBO projectDetailBO) {
        projectDetailBO = ProjectTimeUtil.setProjectDetailBO(projectDetailBO);
        List<ProjectDetailVO> list = targetService.queryAllProjectDetail(projectDetailBO);
//        获取页面下方明细信息
        PageInfo<ProjectDetailVO> projectDetailVOPageInfo = PageHelperTool.excutePageInfo(list,
                projectDetailBO.getPageNo(),
                projectDetailBO.getPageSize());
        ProjectDetailDTO projectDetailDTO = new ProjectDetailDTO();
        projectDetailDTO.setPageInfo(projectDetailVOPageInfo);
        List<ProjectDetailVO> projectDetailVOList = list;
        BigDecimal countMoney = BigDecimal.ZERO;

        //收入类型列表
        List<Long> incomeTypeList = new ArrayList<>();

        //支出类型列表
        List<Long> payoutTypeList = new ArrayList<>();

        //其他类型列表
        List<Long> otherList = new ArrayList<>();

        Collections.addAll(incomeTypeList, 100301L, 100302L);
        Collections.addAll(payoutTypeList, 100303L, 100304L);
        Collections.addAll(otherList, 9999L);
        if (projectDetailVOList.size() != 0) {
            if (projectDetailBO.getExpenseTypeCode() == null) {
                //全部
                BigDecimal incomeTotal = projectDetailVOList.stream()
                        .filter(t -> incomeTypeList.contains(t.getReExTypeCode()))
                        .map(t -> t.getBalance())
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal payoutTotal = projectDetailVOList.stream()
                        .filter(t -> payoutTypeList.contains(t.getReExTypeCode()))
                        .map(t -> t.getBalance())
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal otherTotal = projectDetailVOList.stream()
                        .filter(t -> otherList.contains(t.getReExTypeCode()))
                        .map(t -> t.getBalance())
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                countMoney = incomeTotal.subtract(payoutTotal).subtract(otherTotal);
            } else {
                //支出
                countMoney = projectDetailVOList.stream()
                        .map(t -> t.getBalance())
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
            }
        }

        projectDetailDTO.setSumMoney(countMoney);
        return ResultObject.success(projectDetailDTO);
    }

    @PostMapping("/queryAllProjectDetailOn")
    @ApiOperation("查询明细信息，模态窗口上半部分页面")
    public ResultObject queryAllProjectDetailOn(@RequestBody ProjectDetailBO projectDetailBO) {
        projectDetailBO = ProjectTimeUtil.setProjectDetailBO(projectDetailBO);
        //        获取页面上方信息
        projectDetailBO.setStartNextMonthTime(projectDetailBO.getStartTime()
                .with(TemporalAdjusters.firstDayOfNextMonth()));
        projectDetailBO.setEndNextMonthTime(projectDetailBO.getEndTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        ZxFinicialDetailVO zxFinicialDetailVO = targetService.queryFinicialDetail(projectDetailBO);
        Map<String, Object> map = new HashMap<>();
        map.put("high", zxFinicialDetailVO);
        map.put("down", zxFinicialDetailVO);
        return ResultObject.success(map);
    }

    @PostMapping("/downloadAllProject")
    @ApiOperation("下载外部文件，必要参数HttpServletResponse response,下载的话第几页，每页几条信息就不要了,日期必须要")
    public void downloadFile(@RequestBody ProjectAllBO projectAllBO, HttpServletResponse response,
                             HttpServletRequest request) throws Exception {
        targetService.downloadFile(projectAllBO, response,request);
    }

    @PostMapping("/downloadAllDetailProject")
    @ApiOperation("下载内部文件，必要参数HttpServletResponse response,下载的话第几页，每页几条信息就不要了,日期必须要")
    public void downloadDetailFile(
            @RequestBody ProjectDetailBO projectDetailBO, HttpServletResponse response) throws Exception {
        projectDetailBO = ProjectTimeUtil.setProjectDetailBO(projectDetailBO);
        targetService.downloadDetailFile(projectDetailBO, response);
    }
}

